package com.yxx.data.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxx.data.domain.EconomicStatus;
import com.yxx.data.service.EconomicStatusService;
import com.yxx.data.vo.EconomicStatusVo;
import com.yxx.sys.common.AppSecureUtils;
import com.yxx.sys.common.DataGridView;
import com.yxx.sys.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxx
 * @since 2022-07-09
 */
@RestController
@RequestMapping("/economic-status")
@Api("社会经济地位控制器")
public class EconomicStatusController {
  @Autowired
  EconomicStatusService economicStatusService;

  @GetMapping("/loadAllInformation")
  @ApiOperation("查询社会经济地位")
  public DataGridView loadAllInformation(EconomicStatusVo economicStatusVo) {
    IPage<EconomicStatus> page = new Page<EconomicStatus>(economicStatusVo.getPage(), economicStatusVo.getLimit());
    QueryWrapper<EconomicStatus> queryWrapper = new QueryWrapper<EconomicStatus>();
    queryWrapper.eq(StrUtil.isNotBlank(economicStatusVo.getHospitalId()), "hospital_id", economicStatusVo.getHospitalId());
    queryWrapper.orderByDesc("id");
    economicStatusService.page(page, queryWrapper);
    return new DataGridView(page.getTotal(), page.getRecords());
  }

  @PostMapping("/addInformation")
  @ApiOperation("添加社会经济地位")
  public Result addInformation(EconomicStatusVo economicStatusVo) {
    economicStatusVo.setName(AppSecureUtils.getEncryptData(economicStatusVo.getName()));
    economicStatusVo.setCreatedTime(LocalDate.now());
    economicStatusVo.setChangeTime(LocalDate.now());
    boolean succ = economicStatusService.save(economicStatusVo);
    if(!succ) return Result.ADD_REEOR;
    return Result.ADD_SUCCESS;
  }

  @PostMapping("/updateInformation")
  @ApiOperation("更新社会经济地位")
  public Result updateInformation(EconomicStatusVo economicStatusVo) {
    try {
      boolean b = economicStatusService.updateById(economicStatusVo);
      if(!b) return Result.UPDATE_REEOR;
      return Result.UPDATE_SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      return Result.UPDATE_REEOR;
    }
  }

  @PostMapping("/deleteInformation")
  @ApiOperation("删除社会经济地位")
  public Result deleteInformation(Integer id) {
    try {
      economicStatusService.removeById(id);
      return Result.DELETE_SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      return Result.DELETE_REEOR;
    }
  }

}
